package cn.ruanyun.backInterface.modules.business.versions.serviceimpl;

import cn.ruanyun.backInterface.common.utils.*;
import cn.ruanyun.backInterface.common.vo.Result;
import cn.ruanyun.backInterface.modules.business.versions.mapper.VersionsMapper;
import cn.ruanyun.backInterface.modules.business.versions.pojo.Versions;
import cn.ruanyun.backInterface.modules.business.versions.service.IVersionsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;


/**
 * 设备版本接口实现
 *
 * @author z
 */
@Slf4j
@Service
@Transactional
public class IVersionsServiceImpl extends ServiceImpl<VersionsMapper, Versions> implements IVersionsService {


    @Autowired
    private SecurityUtil securityUtil;

    @Override
    public void insertOrderUpdateVersions(Versions versions) {

        if (ToolUtil.isEmpty(versions.getId())) {

            versions.setCreateBy(securityUtil.getCurrUser().getId());
        } else {

            versions.setUpdateBy(securityUtil.getCurrUser().getId());
        }
        this.saveOrUpdate(versions);
    }

    @Override
    public void removeVersions(String ids) {

        CompletableFuture.runAsync(() -> this.removeByIds(ToolUtil.splitterStr(ids)));
    }


    /**
     * 查询当前系统版本
     *
     * @param currentVersion 当前版本号
     * @return
     */
    @Override
    public Result<Object> getVersions(String currentVersion) {
        Versions v = this.getOne(new QueryWrapper<Versions>().lambda()
                .orderByDesc(Versions::getCreateTime)
                .last("limit 1")
        );

        if (ToolUtil.isNotEmpty(v)&&ToolUtil.isNotEmpty(currentVersion)) {
            Integer current = new Integer(currentVersion.replace(".", ""));
            Integer max = new Integer(v.getNewVersion().replace(".", ""));
            Integer min = new Integer(v.getMinVersion().replace(".", ""));

            if (min > current) {
                v.setIsUpdate(true);
                v.setForceUpdate(true);
                return new ResultUtil<>().setData(v, "当前版本需要更新后才能使用！");

            }else if(min < current && max >current){

                v.setIsUpdate(true);
                return new ResultUtil<>().setData(v, "当前版本需要更新！");

            }else if(max.equals(current)){
                return new ResultUtil<>().setData(v, "当前版本无需更新！");

            }else {
                return new ResultUtil<>().setData(v, "版本数据有误！");
            }

        }else if (ToolUtil.isNotEmpty(v)&&ToolUtil.isEmpty(currentVersion)){
            return new ResultUtil<>().setData(v,"获取数据成功");
        }else {
            return new ResultUtil<>().setErrorMsg(201,"暂无数据！");
        }


    }


}